package cn.itsource.service.impl;

import cn.itsource.common.AjaxResult;
import cn.itsource.common.PageResult;
import cn.itsource.domain.TDepartment;
import cn.itsource.mapper.DepartmentMapper;
import cn.itsource.query.DepartmentQuery;
import cn.itsource.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Effect:
 * date: 2022/1/13 16:56
 *
 * @author wangyu
 * @since JDK 1.8
 */
@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    public List<TDepartment> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public PageResult<TDepartment> pageList(DepartmentQuery query) {
        PageHelper.startPage(query.getPageNum(),query.getPageSize());

        PageInfo<TDepartment> pagelist = new PageInfo<>(departmentMapper.findByQuery(query));

        return new PageResult<>(pagelist.getTotal(),pagelist.getList());
    }

    @Override
    public AjaxResult deleteDeptById(Long id) {
        departmentMapper.deleteDeptById(id);
        return null;
    }

    @Override
    public TDepartment findDeptById(Long id) {
        return departmentMapper.findDeptById(id);
    }

    @Override
    public AjaxResult add(TDepartment form) {
        try {
            String[] split = form.getDirPath().split(",");
            form.setParentId(Long.valueOf(split[split.length-1]));
            departmentMapper.add(form);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }

    }

    @Override
    public AjaxResult update(TDepartment form) {
        try {
            departmentMapper.update(form);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }

    }

    @Override
    public AjaxResult batchDel(List<Long> ids) {
        try {
            departmentMapper.batchDel(ids);
            return AjaxResult.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败");
        }

    }

    @Override
    public List<TDepartment> findTree() {
        List<TDepartment> topList = departmentMapper.findTop();
        for (TDepartment tDepartment : topList) {
            findChildren(tDepartment);

        }
        return topList;
    }

    private void findChildren(TDepartment tDepartment){
        List<TDepartment> children = departmentMapper.findChildren(tDepartment.getId());
        if (CollectionUtils.isEmpty(children)){
            tDepartment.setChildren(null);
        }else {
            tDepartment.setChildren(children);
        }
        tDepartment.setChildren(children);
        for (TDepartment child : children) {
            findChildren(child);
        }

    }
}
